package edu.uwosh.cs342.project3;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MultipleChoice extends Activity implements OnCheckedChangeListener{

	int point = 0;
	String userAnswer = "";
	String[] choices;
	
	boolean[] submitArray;
	int questionPosition;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multi);
		
		Bundle extras = getIntent().getExtras();
		
		//  Get question and answer from extra
		String question = extras.getString("question");
		String correctAnswer = extras.getString("correctAnswer");
		String otherAnswer1 = extras.getString("choice1");
		String otherAnswer2 = extras.getString("choice2");
		String otherAnswer3 = extras.getString("choice3");
		submitArray = extras.getBooleanArray("submitArray");
		questionPosition = extras.getInt("position");
		
		final String answer = correctAnswer;
		
		//  Setting up the question
		TextView questionTitle = (TextView) findViewById(R.id.multipleChoiceTextView);
		questionTitle.setText(question);
		
		//  Setting up the radio group
		RadioGroup multipleChoiceGroup = (RadioGroup) findViewById(R.id.multipleChoiceRadioGroup);
		multipleChoiceGroup.clearCheck();
		multipleChoiceGroup.setOnCheckedChangeListener(this);
		
		//  Randomize choices
		choices = new String[]{correctAnswer, otherAnswer1, otherAnswer2, otherAnswer3};
		Collections.shuffle(Arrays.asList(choices));
		
		//  Put choices in radio buttons
		RadioButton radioA = (RadioButton) findViewById(R.id.choiceARadioButton);
		RadioButton radioB = (RadioButton) findViewById(R.id.choiceBRadioButton);
		RadioButton radioC = (RadioButton) findViewById(R.id.choiceCRadioButton);
		RadioButton radioD = (RadioButton) findViewById(R.id.choiceDRadioButton);
		radioA.setText(choices[0]);
		radioB.setText(choices[1]);
		radioC.setText(choices[2]);
		radioD.setText(choices[3]);
		
		//  Setting up the button
		Button multipleChoiceSubmitButton = (Button) findViewById(R.id.multipleChoiceSubmitButton);
		multipleChoiceSubmitButton.setOnClickListener(new View.OnClickListener() {
	      	public void onClick(View v){
	       		if(userAnswer.equals("")){
	       			;
	       		}
	       		else
	       		{
	       			//  Return one point if student is correct
	       			if (userAnswer.equals(answer)) {
	       				point = 1;
	       			}			
	       			submitArray[questionPosition] = true;
	       			
	       			finish();
	       		}
	       	}
	       		
		});
	}

	public void onCheckedChanged(RadioGroup group, int checkedId) {
		//  Change user's answer when radio button is clicked
		switch(checkedId){
		case R.id.choiceARadioButton:
			userAnswer = choices[0];
			break;
		case R.id.choiceBRadioButton:
			userAnswer = choices[1];
			break;
		case R.id.choiceCRadioButton:
			userAnswer = choices[2];
			break;
		case R.id.choiceDRadioButton:
			userAnswer = choices[3];
			break;
		}
	}
	
	@Override
	public void finish() {
		Intent data = new Intent();
		data.putExtra("point", point);
		data.putExtra("returnSubmit", submitArray);
		//data.putExtra("returnPosition", questionPosition);
		
		// Activity finished ok, return the data
		setResult(RESULT_OK, data);
		
		super.finish();	
	}
}
